package 力扣日常刷题.木22一月.第04天0111;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/1/11
 * @description
 */
public class Demo02回溯中等全排列 {

    boolean isUsed[];
    ArrayList<List<Integer>> ans = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        isUsed = new boolean[nums.length];
        dfs(nums,0,new ArrayList<Integer>());
       return ans;
    }

    void dfs(int[] nums,int index,ArrayList<Integer> list){
        if(index == nums.length){
            ans.add(new ArrayList<Integer>(list));
        }
        for (int i = 0; i < nums.length; i++) {
            if(!isUsed[i]){
                list.add(nums[i]);
                isUsed[i] = true;
                dfs(nums,index+1,list);
                list.remove(list.indexOf(nums[i]));
                isUsed[i] = false;
            }
        }
    }

}
